package com.apache.common;

import java.io.FileInputStream;
import java.util.Properties;

import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
 * 
 * <p>@Description 邮件服务帮助类
 * <p>@Copyright(c)2013:东吴软件技术有限公司
 * <p>@Author: lvcai
 * <p>@Create Date 2013-7-30 上午11:08:24
 * <p>@Modified By:lvcai
 * <p>@Modified Date:2013-7-30 上午11:08:24
 * <p>@version v1.0
 */
public class Mailer {
	private static Logger logger = LoggerFactory.getLogger(Mailer.class);
	private static String host = "";//邮件服务器主机地址

	private static String account = ""; //发送邮件的账号
 
	private static String password = "";//发送邮件的密码

	private static String from_email = ""; //发送者的email地址
	
	private static String defaultSubject = ""; //默认主题	

	private static final String charset = "GB2312";	
	
	//-----------------------------------------------

	/**邮件内容*/
	protected String tcontent; 

	/**邮件主题*/
	protected String subject;

	/**接收者 email 地址*/
	protected String tto;

	/**抄送给一个email地址*/
	protected String BBcc;

	/**抄送给多个email地址*/
	protected String[] CC = null;

	/**
	 * 
	 */
	public Mailer() {
		logger.info("init Mailler");
		try {
			String path = Mailer.class.getResource("/").getPath()+"pbo_mail.properties";
			FileInputStream is = new FileInputStream(path);
			
			Properties prop = new Properties();
			prop.load(is);

			host = prop.get("host").toString();
			account = prop.get("account").toString();
			password = prop.get("password").toString();
			from_email = prop.get("from").toString();
			defaultSubject = prop.get("subject").toString();
			
			logger.info("init pbo_mail.properties ok.");

		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public void setcontent(String content) {
		this.tcontent = content;
	}
	public void setSubject(String subject) {
		this.subject = subject;
	}
	public void setto(String to) {
		this.tto = to;
	}
	public void setCC(String[] cc) {
		this.CC = cc;
	}
	
	public boolean Send() {
		try {
			Properties props = new Properties();
			props.put("mail.smtp.localhost", host);
			props.put("mail.smtp.auth", "true");
			
			Session session = Session.getInstance(props);
			session.setDebug(false);//是否打印调试信息
			
			MimeMessage message = new MimeMessage(session);
			InternetAddress from = new InternetAddress(from_email);
			message.setFrom(from);//
			
			InternetAddress to = new InternetAddress(tto);
			message.setRecipient(Message.RecipientType.TO, to);
			if (null != BBcc) {
				InternetAddress Bcc = new InternetAddress(BBcc);
				message.setRecipient(Message.RecipientType.BCC, Bcc);
			}
			if (null != CC) {
				int adlength = CC.length;
				// 为每个邮件接收者创建一个地址-new
                Address[] ccAdresses = new InternetAddress[adlength];
				for (int i = 0; i < adlength; i++) {
					ccAdresses[i] = new InternetAddress(CC[i]);
				}
				  // 将抄送者信息设置到邮件信息中，注意类型为Message.RecipientType.CC
				message.setRecipients(Message.RecipientType.CC, ccAdresses);

			}
	
			BodyPart mdp = new MimeBodyPart();	
			mdp.setContent(tcontent, "text/html;charset=" + charset);
			Multipart mm = new MimeMultipart();
			mm.addBodyPart(mdp);
			message.setContent(mm);
			message.saveChanges();
			subject = subject==null?defaultSubject:subject ;			
			
			// 中文编码
			if (null != subject) {
				try {
					sun.misc.BASE64Encoder enc = new sun.misc.BASE64Encoder();
					subject = "=?GB2312?B?"	+ enc.encode(subject.getBytes("GB2312")) + "?=";
				} catch (Exception e) {
					message.setSubject("");
				}
			}
			
			message.setSubject(subject);
			
			Transport transport = session.getTransport("smtp");
			transport.connect(host, account, password);
			logger.info("是否连接"+transport.isConnected());
			transport.sendMessage(message, message.getAllRecipients());
			transport.close();
			
			logger.info("send email ok: "+to);
			
		} catch (Exception e) {
			e.printStackTrace();
			logger.info("sendmail error :" + e.getMessage());
			return false;
		}
		return true;
	}
	
	
	public static void main(String args[]){
		Mailer mail = new Mailer();
		mail.setcontent("testetestset");
		mail.setSubject("您的密码为");
		mail.setto("qmhao@zbxsoft.com");
		
		mail.Send();
		
	}
}
